using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Comteck.Winforms.Print {
  public class StringConvert {
    /// <summary>
    /// ASCII字符串转换为字节数组
    /// </summary>
    /// <param name="asciistr"></param>
    /// <param name="bytes"></param>
    /// <returns></returns>
    public static Boolean StringToBytes(String asciistr, out Byte[] bytes) {
      bytes = Encoding.ASCII.GetBytes(asciistr);
      if (bytes == null || bytes.Length < 1) {
        return false;
      }
      return true;
    }


    /// <summary>
    /// decimal转bytes
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static Byte[] ToBytes(string str) {
      byte[] bytes = new byte[str.Length * sizeof(char)];
      System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
      return bytes;
    }

    /// <summary>
    /// 将字节数组转换为<c>Decimal</c>对象。
    /// </summary>
    /// <param name="array">要转换的字节数组。</param>
    /// <returns>所转换的<c>Decimal</c>对象。</returns>
    public static string FromBytes(Byte[] bytes) {
      char[] chars = new char[bytes.Length / sizeof(char)];
      System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
      return new string(chars);
    }



    /// <summary>
    ///  字符串转byte    "34"---->52 (0x34)
    /// </summary>
    /// <param name="strAsccii"></param>
    /// <returns></returns>
    public static byte ConvertAscciiToByte(string strAsccii) {
      strAsccii = strAsccii.Trim();
      if (strAsccii.Length > 2 || strAsccii.Length < 1) return 0x00;
      return byte.Parse(strAsccii, System.Globalization.NumberStyles.AllowHexSpecifier);
    }

    /// <summary>
    /// 十六进制数字字符串转byte数组
    /// </summary>
    /// <param name="hexstr"></param>
    /// <returns></returns>
    public static byte[] HexStringToBytes(string hexstr) {
      byte[] bytes = null;
      if (String.IsNullOrWhiteSpace(hexstr) || (hexstr.Length < 2)) {
        return bytes;
      }

      string tmpstr = hexstr.Replace(" ", "");

      if (tmpstr.Length % 2 == 1) {
        return bytes;
      }
      int bytelen = tmpstr.Length / 2;
      if (bytelen < 1) {
        return bytes;
      }
      bytes = new Byte[bytelen];

      String curbytestr = String.Empty;
      for (int bidx = 0; bidx < bytelen; bidx++) {
        curbytestr = tmpstr.Substring(bidx * 2, 2);
        //数字的进制，它必须是 2、8、10和16.
        bytes[bidx] = Convert.ToByte(curbytestr, 16);
      }

      return bytes;
    }

    /// <summary>
    /// byte[]流转十六进制字符串 0x34---->"34"
    /// </summary>
    /// <param name="bytes"></param>
    /// <returns></returns>
    public static string ToHexString(byte[] bytes) {
      char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
      char[] chars = new char[bytes.Length * 2];
      for (int i = 0; i < bytes.Length; i++) {
        int b = bytes[i];//十六进制转化为十进制 0x34->52
        chars[i * 2] = hexDigits[b >> 4];
        chars[i * 2 + 1] = hexDigits[b & 0xF];
      }
      return new string(chars);
    }
  }
}
